package org.ruoyi.dhuman.service.impl;

import org.ruoyi.common.core.utils.MapstructUtils;
    import org.ruoyi.core.page.TableDataInfo;
    import org.ruoyi.core.page.PageQuery;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.ruoyi.dhuman.domain.bo.DhumanPptBo;
import org.ruoyi.dhuman.domain.vo.DhumanPptVo;
import org.ruoyi.dhuman.domain.DhumanPpt;
import org.ruoyi.dhuman.mapper.DhumanPptMapper;
import org.ruoyi.dhuman.service.IDhumanPptService;
import org.ruoyi.common.core.utils.StringUtils;

import java.util.List;
import java.util.Collection;

/**
 * 数字人讲PPTService业务层处理
 *
 * @author wangliang
 * @date Tue Oct 14 12:06:13 CST 2025
 */
@RequiredArgsConstructor
@Service
public class DhumanPptServiceImpl implements IDhumanPptService {

    private final DhumanPptMapper baseMapper;

    /**
     * 查询数字人讲PPT
     */
    @Override
    public DhumanPptVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

	/**
	 * 查询数字人讲PPT列表
	 */
	@Override
	public TableDataInfo<DhumanPptVo> queryPageList(DhumanPptBo bo, PageQuery pageQuery) {
		LambdaQueryWrapper<DhumanPpt> lqw = buildQueryWrapper(bo);
		Page<DhumanPptVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
		return TableDataInfo.build(result);
	}

    /**
     * 查询数字人讲PPT列表
     */
    @Override
    public List<DhumanPptVo> queryList(DhumanPptBo bo) {
        LambdaQueryWrapper<DhumanPpt> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DhumanPpt> buildQueryWrapper(DhumanPptBo bo) {
        LambdaQueryWrapper<DhumanPpt> lqw = Wrappers.lambdaQuery();
                    lqw.eq(bo.getId() != null, DhumanPpt::getId, bo.getId());
                    lqw.eq(StringUtils.isNotBlank(bo.getTenantId()), DhumanPpt::getTenantId, bo.getTenantId());
                    lqw.eq(StringUtils.isNotBlank(bo.getPptId()), DhumanPpt::getPptId, bo.getPptId());
                    lqw.eq(StringUtils.isNotBlank(bo.getPptName()), DhumanPpt::getPptName, bo.getPptName());
                    lqw.eq(StringUtils.isNotBlank(bo.getPptUrl()), DhumanPpt::getPptUrl, bo.getPptUrl());
                    lqw.eq(StringUtils.isNotBlank(bo.getVideoName()), DhumanPpt::getVideoName, bo.getVideoName());
                    lqw.eq(StringUtils.isNotBlank(bo.getDhumanVersion()), DhumanPpt::getDhumanVersion, bo.getDhumanVersion());
                    lqw.eq(bo.getDhumanId() != null, DhumanPpt::getDhumanId, bo.getDhumanId());
                    lqw.eq(StringUtils.isNotBlank(bo.getDhumanImage()), DhumanPpt::getDhumanImage, bo.getDhumanImage());
                    lqw.eq(StringUtils.isNotBlank(bo.getDhumanAudio()), DhumanPpt::getDhumanAudio, bo.getDhumanAudio());
                    lqw.eq(bo.getDhumanX() != null, DhumanPpt::getDhumanX, bo.getDhumanX());
                    lqw.eq(bo.getDhumanY() != null, DhumanPpt::getDhumanY, bo.getDhumanY());
                    lqw.eq(bo.getDhumanZoom() != null, DhumanPpt::getDhumanZoom, bo.getDhumanZoom());
                    lqw.eq(StringUtils.isNotBlank(bo.getStatus()), DhumanPpt::getStatus, bo.getStatus());
                    lqw.eq(bo.getStartTime() != null, DhumanPpt::getStartTime, bo.getStartTime());
                    lqw.eq(bo.getEndTime() != null, DhumanPpt::getEndTime, bo.getEndTime());
                    lqw.eq(StringUtils.isNotBlank(bo.getVideoUrl()), DhumanPpt::getVideoUrl, bo.getVideoUrl());
                    lqw.eq(bo.getCreateBy() != null, DhumanPpt::getCreateBy, bo.getCreateBy());
                    lqw.eq(bo.getCreateTime() != null, DhumanPpt::getCreateTime, bo.getCreateTime());
                    lqw.eq(bo.getUpdateBy() != null, DhumanPpt::getUpdateBy, bo.getUpdateBy());
                    lqw.eq(bo.getUpdateTime() != null, DhumanPpt::getUpdateTime, bo.getUpdateTime());
                    lqw.eq(StringUtils.isNotBlank(bo.getRemark()), DhumanPpt::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增数字人讲PPT
     */
    @Override
    public Boolean insertByBo(DhumanPptBo bo) {
        DhumanPpt add = MapstructUtils.convert(bo, DhumanPpt. class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改数字人讲PPT
     */
    @Override
    public Boolean updateByBo(DhumanPptBo bo) {
        DhumanPpt update = MapstructUtils.convert(bo, DhumanPpt. class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DhumanPpt entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除数字人讲PPT
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
